/*solves the sudoku*/
package sudokusolver;

import java.io.FileReader;
import java.io.File;
import java.io.FileNotFoundException;
public class SudokuSolver {
    public static void solve(File f)throws Exception {
        FileReader file=new FileReader(f);//file to be read from.
        Grid grid=new Grid();//grid to be populated
        char c;
        for (int i = 0; i < Grid.NO_OF_ROWS; i++) {
            for (int j = 0; j < Grid.NO_OF_COLUMNS; j++) {
                c=(char)(file.read());
                if(c=='*') {//empty cell
                    grid.grid[i][j]=new Cell(grid,i,j);
                }
                else {
                    grid.grid[i][j]=new Cell(grid,Integer.parseInt(c+""),i,j);                    
                }
            }
            c=(char)(file.read());//throws out the /n character.
        }
        for (int alv=0;alv<Grid.NO_OF_ROWS;alv++) {
            for (int yalv=0;yalv<Grid.NO_OF_COLUMNS;yalv++) {
                grid.grid[alv][yalv].removeMarkupsFromAllOfThisCellsBuddies();
            }
        }
        int loopvar=0;
        long thisTime=System.nanoTime();
        while (!(grid.isSolved())) {
            for (int r = 0; r < Grid.NO_OF_ROWS; r++) {
                for (int s = 0; s < Grid.NO_OF_COLUMNS; s++) {
                    grid.grid[r][s].updateMarkup();
                    grid.grid[r][s].removeMarkupsFromAllOfThisCellsBuddies();
                }
            }
            grid.printMarkup();
            System.out.println(loopvar);
            if(loopvar%2==0) {
                for (int a=0;a<Grid.NO_OF_ROWS;a++) {
                    for (int b = 0; b < Grid.NO_OF_COLUMNS; b++) {
                        grid.grid[a][b].initializeFinalValue();
                        grid.grid[a][b].removeMarkupsFromAllOfThisCellsBuddies();
                    }
                }
            }
            loopvar++;
        }
        for (int e=0;e<Grid.NO_OF_ROWS;e++) {
                 for (int d = 0;d < Grid.NO_OF_COLUMNS;d++) {
                     grid.grid[e][d].initializeFinalValue();
                 }
        }
        grid.printGrid();
        long thatTime=System.nanoTime();
        System.out.println(thatTime-thisTime);
    }
    public static void main(String[] args) throws Exception {
            solve(new File("./samples/Wiki1"));
            solve(new File("./samples/TomDavis1"));
    }
}
